package nc.appcan.sp.bs.tool;

import nc.ui.pub.print.IDataSource;
import nc.vo.pub.AggregatedValueObject;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.lang.UFDouble;

public class PadPrintdataImpl implements IDataSource {

	private static final long serialVersionUID = 1L;

	private AggregatedValueObject m_voAgg = null;

	private String HEAD = "h_";
	private String TAIL = "t_";

	public PadPrintdataImpl(AggregatedValueObject avo) {
		super();
		m_voAgg = avo;
	}

	public String[] getAllDataItemExpress() {
		int parentCount = 0;
		int childrenCount = 0;
		if (m_voAgg.getParentVO() != null) {
			parentCount = m_voAgg.getParentVO().getAttributeNames().length;
		}
		if (m_voAgg.getChildrenVO() != null) {
			childrenCount = m_voAgg.getChildrenVO()[0].getAttributeNames().length;
		}
		int count = parentCount + childrenCount;
		String[] expfields = new String[count];
		try {
			for (int i = 0; i < parentCount; i++) {
				expfields[i] = HEAD
						+ m_voAgg.getParentVO().getAttributeNames()[i];
			}
			for (int j = 0; j < childrenCount; j++) {
				expfields[j + parentCount] = m_voAgg.getChildrenVO()[0]
						.getAttributeNames()[j];
			}
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
		return expfields;
	}

	public String[] getAllDataItemNames() {
		int parentCount = 0;
		int childrenCount = 0;
		if (m_voAgg.getParentVO() != null) {
			parentCount = m_voAgg.getParentVO().getAttributeNames().length;
		}
		if (m_voAgg.getChildrenVO() != null) {
			childrenCount = m_voAgg.getChildrenVO()[0].getAttributeNames().length;
		}
		int count = parentCount + childrenCount;
		String[] namefields = new String[count];
		try {
			for (int i = 0; i < parentCount; i++) {
				namefields[i] = HEAD
						+ m_voAgg.getParentVO().getAttributeNames()[i];
			}
			for (int j = 0; j < childrenCount; j++) {
				namefields[j + parentCount] = m_voAgg.getChildrenVO()[0]
						.getAttributeNames()[j];
			}
		} catch (Throwable e) {
			throw new RuntimeException(e);
		}
		return namefields;
	}

	public String[] getDependentItemExpressByExpress(String itemExpress) {
		return null;
	}

	public String[] getItemValuesByExpress(String itemExpress) {
		if (m_voAgg == null || itemExpress == null) {
			return null;
		}
		if (itemExpress.startsWith(HEAD) || itemExpress.startsWith(TAIL)) {
			if (m_voAgg.getParentVO() == null) {
				return null;
			}
			return new String[] { getString_TrimZeroLenAsNull(m_voAgg
					.getParentVO().getAttributeValue(
							itemExpress.substring(HEAD.length()))) };
		} else {
			CircularlyAccessibleValueObject[] voaBody = m_voAgg.getChildrenVO();
			if (voaBody == null || voaBody.length == 0) {
				return null;
			}
			String[] saRet = new String[voaBody.length];
			for (int i = 0; i < voaBody.length; i++) {
				saRet[i] = (voaBody[i] == null) ? "" : getString_TrimZeroLenAsNull(voaBody[i]
								.getAttributeValue(itemExpress));
			}
			return saRet;
		}
	}

	public String getModuleName() {
		return "";
	}

	public boolean isNumber(String itemExpress) {
		if (itemExpress.startsWith(HEAD) || itemExpress.startsWith(TAIL)) {
			Object obj = m_voAgg.getParentVO().getAttributeValue(itemExpress);
			Class itemClass = obj == null ? Object.class : obj.getClass();
			if (itemClass.equals(UFDouble.class)
					|| itemClass.equals(Integer.class)) {
				return true;
			} else {
				return false;
			}
		} else {
			Object obj = m_voAgg.getChildrenVO()[0]
					.getAttributeValue(itemExpress);
			Class itemClass = obj == null ? Object.class : obj.getClass();
			if (itemClass.equals(UFDouble.class)
					|| itemClass.equals(Integer.class)) {
				return true;
			} else {
				return false;
			}
		}
	}
	private String getString_TrimZeroLenAsNull (Object o){
		if(o==null)return null;
		return ""+o;
	}
}
